﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using CRMBiz;
using Newtonsoft.Json;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for SMSTemplate
    /// </summary>
    public class SMSTemplate : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }
        public void SMSTemplateShowAll()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(SMSTemplateHandler.BindSMSTemplate());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
        public int SMSTemplateAdd(string SMSTemplate, string TemplateFormat, int CategoryID, string SMSCompany)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = SMSTemplateHandler.AddSMSTemplate(SMSTemplate, TemplateFormat, CategoryID, SMSCompany);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }


        public int UpdateSMSTemplateDual(int SMSTemplateID, string TemplateFormat, int CategoryID)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = SMSTemplateHandler.UpdateSMSTemplateDual(SMSTemplateID, TemplateFormat, CategoryID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public int SMSTemplateUpdate(int SMSTemplateID, string TemplateFormat, int CategoryID, string SMSCompany)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = SMSTemplateHandler.UpdateSMSTemplate(SMSTemplateID, TemplateFormat, CategoryID, SMSCompany);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        public bool SMSTemplateDelete(int SMSTemplateID)
        {
            bool flag = false;
            try
            {
                flag = SMSTemplateHandler.DeleteSMSTemplate(SMSTemplateID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        public void getColumnName(string FilterCondition)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(SMSTemplateHandler.getColumnName(FilterCondition));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void getColumnName1()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(SMSTemplateHandler.getColumnName1());
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        public void DisplayTemplateNode(string Company)
        {
            try
            {
                bool isSMS = true;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetTemplateCategoryNodesList(isSMS, false, false,Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }
        public void DisplayCategoryList(string Company)
        {
            try
            {
                bool isSMS = true;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetTemplateCategoryNodesList(isSMS, true, false, Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }


        public void GetSMSTemplateContentById(int smsTemplateId)
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetSMSTemplateContent(smsTemplateId));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }


        //Display sms dialog template nodes:
        public void DisplayTemplateNodeForDialog(string Company)
        {
            try
            {
                bool isSMS = true;
                string strJson = JsonConvert.SerializeObject(CategoryNodeHandler.GetCategoryNodeForDialog(isSMS, false, false, Company));
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }


        //Display email data list:
        public void GetSMSDataList(string Company,int CategoryID)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = SMSTemplateHandler.GetSMSDataList(pageIndex, pageSize,Company,CategoryID);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
        }

        //Delete list data:
        public bool DeleteSMSData(int SMSTemplateID)
        {
            bool flag = false;
            try
            {
                flag = SMSTemplateHandler.DeleteSMSData(SMSTemplateID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex,CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }
    }
}